package com.VisualDisp.QuadCalculateProject.VisualDisplay;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import com.VisualDisp.QuadCalculateProject.Calculations.Equations.Expressions;
import com.VisualDisp.QuadCalculateProject.VisualDisplay.GraphShell.LineDraw;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Plot extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private  JPanel panel;
	private Graphics g2d;
	private LineDraw lineDraw;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Plot dialog = new Plot();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Plot() {
		Init();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public Plot(Expressions exp){
		this();
		this.lineDraw = new LineDraw(exp);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void setVisible(boolean flag)
	{
		super.setVisible(flag);
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(flag)
		{	
			g2d = panel.getGraphics();
			DrawLine();
			if(lineDraw != null)
			{
				lineDraw.assignGraph(g2d);
				lineDraw.setDimensions(this.panel.getSize().getWidth(),this.panel.getSize().getHeight());
				lineDraw.draw();
			}
		}

	}

	private void DrawLine()
	{
		//if(panel.getGraphics() != null)
		//{
		g2d.setColor(Color.BLUE);

		int maxX = (int)panel.getSize().getWidth();
		int maxY = (int)panel.getSize().getHeight();

		int halfX = maxX/2;
		int halfY = maxY/2;

		g2d.drawLine(halfX, 0, halfX, maxY);
		g2d.drawLine(0 , halfY, maxX, halfY);
		//}

	}
	private void Init()
	{
		setBounds(100, 100, 634, 398);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			panel = new JPanel();
			panel.setBackground(Color.WHITE);
		}
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addContainerGap()
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_contentPanel.setVerticalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addGap(5)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
						.addContainerGap())
				);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						DrawLine();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
